// 创建请求
function createRequest(args, isform) {
  if (args.url.indexOf('addFormId') < 0) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
  }
  return function(obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function(res) {
        console.log('======== request请求 ========');
        console.log(args);
        console.log('======== request请求成功 ========');
        console.log(res)
        if (res.statusCode === 200) {
          if (res.data.code === 0) {
            resolve(res.data)
            return;
          } else if (res.data.code === 201) {
            wx.hideLoading();
            wx.setStorageSync('userInfo', null);
            wx.setStorageSync('token', '');
            getApp().globalData.token = '';
            getApp().globalData.userInfo = null;
            getApp().showConfirm('您还未登录', '请先登录再进行操作~', true, function() {
              if (getApp().globalData.openid) {
                wx.navigateTo({
                  url: '/pages/center/bind/bind',
                })
              } else {
                wx.navigateTo({
                  url: '/pages/authorization/authorization',
                })
              }
            }, () => {
              wx.navigateBack()
            }, '暂不登录', '登录注册');
          } else if (res.data.code === 315) {
            wx.hideLoading();
            getApp().showConfirm('提示', res.data.msg, false, function() {
              wx.navigateTo({
                url: '/pages/bind/bind',
              })
            });
          } else {
            getApp().showConfirm('提示', res.data.msg, false);
            wx.hideLoading();
          }
        } else {
          getApp().showConfirm('提示', res.data.errorMessage + ', 请刷新', false);
        }
      }
      obj.fail = function(res) {
        console.log('======== request请求 ========');
        console.log(args);
        console.log('======== request请求失败 ========');
        console.log(res);
        wx.hideLoading();
        reject(res)
      }
      if (!obj.header) {
        obj.header = {}
      }
      if (isform) {
        obj.header['content-type'] = "application/x-www-form-urlencoded";
      }
      var merge = extend(obj, args);
      wx.request(obj)
    })
  }
}

// 创建上传请求
function createUpload(args) {
  return function(obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function(res) {
        if (res.statusCode == 200) {
          var data = JSON.parse(res.data);
          if (data.status == 0) {
            resolve(data);
            return;
          }
        }
        reject(res)
      }
      obj.fail = function(res) {
        reject(res)
      }
      if (!obj.header) {
        obj.header = {}
      }
      var merge = extend(obj, args);
      wx.uploadFile(obj);
    })
  }
}

// 创建多请求
function createAll(args) {
  return Promise.all(args);
}

function extend(des, src, override) {
  if (src instanceof Array) {
    for (var i = 0, len = src.length; i < len; i++)
      extend(des, src[i], override);
  }
  for (var i in src) {
    if (override || !(i in des)) {
      des[i] = src[i];
    }
  }
  return des;
}

module.exports = {
  createRequest: createRequest,
  createUpload: createUpload,
  createAll: createAll,
  extend: extend
}